

/*
* 选中地区时的操作
* */
function cleckArea(th){
	var id = $(th).attr('did');

	var pname = $(th).attr('pname');
	var name = $(th).text();
	var cktype = $(th).attr('cktype');
	$('#des_id').val(id);			
	$("#des").attr('cktype',cktype);
	$('#des').attr('did',id);
	$('#des').val(name+pname);
	$('.addr-city-box').hide();
	try{
		extend(th);//调用扩展JS函数，在每个页面自己新加功能
	}catch(err){
		console.log('extend_err');
	}
}
/*
* input框搜索结果点击事件
*
* */
function clickScreen(th){
	// alert(123);
	var val = $(th).find('.t-ellip').text();
	if(val == ''){
		val = $(th).text();
	}
	var id = $(th).attr('did');
	var cktype = $(th).attr('cktype');
	$('#des_id').val(id);
	$("#des").val(val);
	$("#des").attr('did',id);
	$("#des").attr('cktype',cktype);
	$('.input-search-select').hide();
	try{
		extend(th);//调用扩展JS函数，在每个页面自己新加功能
	}catch(err){
		console.log('extend_err');
	}
}

$(function(){
$(".ser-r-con").mousemove(function(){
	
	$(this).addClass("ser-r-con-hover");
	$(this).find("div").removeClass("hidden");
}).mouseout(function(){
	$(this).removeClass("ser-r-con-hover");
	$(this).find("div").addClass("hidden");
})

$(".se-r-con a").click(function(){
	$("#des").val($(this).attr("data-name"));
	$(".addr-city-box").hide();
})

$(".se-history-li").click(function(){
	$("#des").val($(this).attr("data-name"));
	$(".addr-city-box").hide();
})

})


var num = 0;
function Dialog (opts) {
	if(!opts.source) return;
	this.$source = opts.source;
	this.$pane = opts.pane;
	this.$parent = opts.parent;
	this.$closeBtn = opts.closeBtn || null;
	this.$keyup = opts.keyup || '';
	this.init();
	//消失或隐藏的标志
	this.hideflag = true;
}
var inputVal = '';
/*
* 获取筛选的数据
* */
function getScreenData(val){
	if(inputVal != ''){
		if(inputVal == val){

			return;
		}else{
			inputVal = val;
		}
	}else{
		inputVal = val;
	}
	val = val.split(' · ')[0];
	num = GetRandomNum(1,100000);							
}

/*生成随机数*/
function GetRandomNum(Min,Max)
{
	var Range = Max - Min;
	var Rand = Math.random();
	return(Min + Math.round(Rand * Range));
}


var show_type = 1;
var keyup_type = 1;
Dialog.prototype = {
	constructor: Dialog,
	init: function () {
		//点击显示定位事件
		this.clickShow();
		//点击赋值事件
		this.clickVal();
		//关闭按钮点击事件
		this.closeP();
		//消失或隐藏
		this.hide(this.$pane);
		//keyup搜索下拉显示
		this.keyup();
	},
	clickShow: function () {
		var _this = this;
		//点击显示且定位
		this.$source.bind('click',function () {
				$("#des").select();
				if (!$(this).val()) {
					show_type = 1;
					_this.$pane.show();
					return;
				}
		});
	},
	clickVal: function () {			//选中地区事件
		var _this = this;

		//this.$pane.find('.data-set').bind('click',function () {
		//	var val = $(this).text();
		//	_this.$source.val(val);
		//	_this.$pane.hide();
		//});
	},
	closeP: function () {
		var _this = this;
		this.$closeBtn.bind('click',function () {

			//if(show_type != 2 && val.length < 1) {
				_this.$pane.hide();
			//}
		})
	},
	hide: function (ele) {
		var _this = this;
		ele.bind('mouseenter',function () {
			_this.hideflag = false;
		});
		ele.bind('mouseleave',function () {
			_this.hideflag = true;
		});
		$('body').bind('click',function (e) {					
			e = e || window.event;
			var target = e.target || e.srcElement;
			if(_this.hideflag){
				if($(target).attr('class') != _this.$source.attr('class')){
					ele.hide();
				}
			}
		})
	},
	keyup: function () {			//搜索框输入时事件
		var _this = this;
		this.$source.keyup(function () {
			// $("#des_id").val(0);
			// $("#des").attr('did',0);
			// $("#des_id1").val(0);
			// $("#des1").attr('did',0);
			// $('#des').attr('cktype','');
			// $('#keyword').val('');
			// $('#keyword2').hide();
			try {
				entryExtend();//輸入時調用的擴展函數
			}catch(err){
				console.log(err);
			}
			var val = $(this).val();
			if(val.length) {
				// getScreenData(val);
				show_type=2;
				// getHotelData(_this,val);
			}else{
				show_type=1;
				_this.$keyup.hide();
				_this.$pane.show();
			}
		});
		_this.hide(this.$keyup);
	}
}

var cpLock = false;
// 输入汉语拼音时锁住搜索框，不进行搜索，或者从汉语拼音转到字母时也可触发
$('#des').on('compositionstart', function () {
    cpLock = true;
});

// 结束汉语拼音输入并生成汉字时，解锁搜索框，进行搜索
$('#des').on('compositionend', function () {
    cpLock = false;
    des();
    getHotelData("",$('#des').val());
});

var last;
$('#des').on("input", function () {
    if (!cpLock) {
        last = new Date().getTime() //利用event的timeStamp来标记时间，这样每次事件都会修改last的值，注意last必需为全局变量
        setTimeout(function(){    //设时延迟0.5s执行
            yashi = new Date().getTime();
            if((yashi)*1 - (last)*1 >= 250){
                des();
                value = $('#des').val();
				$(".input-search-select").hide();
                if (value) {
				    getHotelData("",value);
                }else{
                	$(".addr-city-box").show();
                }
            }
        },250);
    }
});

$("#des").click(function(){
	if ($('#des').val()) {
	    getHotelData("",$('#des').val());
	}
})

function des(){
	$("#des_id").val(0);
	$("#des").attr('did',0);
	$("#des_id1").val(0);
	$("#des1").attr('did',0);
	$('#des').attr('cktype','');
	$('#keyword').val('');
	$('#keyword2').hide();
}


var searchNum = 0;
//根据键入关键词模糊查询地区、酒店
function getHotelData(_this,keyword){
	searchNum ++;
	var html = "";			
	$.post('/pub/Search/getSarchCacheData',{keyword:keyword,searchType:searchType,searchNum:searchNum},function(data){	
		if(searchNum != data.searchNum){
			return false;
		}
		var data = data.info;
		var html = structureHtml(data,'dest-sear-contxtA');	

		$('.input-search-select').html(html);

		$(".ser-r-con").mousemove(function(){
			$(this).addClass("ser-r-con-hover");
			$(this).find("div").removeClass("hidden");
		}).mouseout(function(){
			$(this).removeClass("ser-r-con-hover");
			$(this).find("div").addClass("hidden");
		})
		
		$(".se-r-con a").click(function(){
			$("#des").val($(this).attr("data-name"));
			$(".addr-city-box").hide();
		})
		
		$(".se-history-li").click(function(){
			$("#des").val($(this).attr("data-name"));
			$(".addr-city-box").hide();
		})
		$(".ser-l-con").css("height","32px");
		$(".ser-l-con").css("line-height","32px");
		$(".ser-r-con").css("height","32px");
		$(".ser-r-con").css("line-height","32px");

		// _this.$keyup.find('ul').html(html);
		$(".input-search-select").show();
		$(".addr-city-box").hide();
	},'json');			
}





//关键字点击事件
$(document).on("click","#keyword",function(){
	searchNum++;
	if($(this).val()==""){
		// $(".keyword_A").show();
		// $(".keyword_B").hide();
		var keyword = '*';
	}else{
		// $(".keyword_A").hide();
		// $(".keyword_B").show();
		var keyword = $('#keyword').val();
	}

	var cktype = 'area';
	var des_id = $("#des_id").val();
	
	$.post('/pub/Search/getSarchKeyCacheData',{keyword:keyword,searchType:'hotel',searchNum:searchNum,cktype:cktype,des_id:des_id},function(data){	
		if(searchNum != data.searchNum){
			return false;
		}
		var data = data.info;
		var html = structureHtml(data,'dest-sear-contxtB');	
		$(".keyword_B").html(html);
		$(".keyword_B").show();

	},'json')

})

var isLock = false;
// 输入汉语拼音时锁住搜索框，不进行搜索，或者从汉语拼音转到字母时也可触发
$('#keyword').on('compositionstart', function () {
    isLock = true;
});

// 结束汉语拼音输入并生成汉字时，解锁搜索框，进行搜索
$('#keyword').on('compositionend', function () {
    isLock = false;
    $("#des").attr('cktype','area');
	$("#des_id1").val('');
	searchNum++;
    if($('#keyword').val()==''){
		var keyword = '*';
    }else{
		var keyword = $('#keyword').val();
    }
	var cktype = $("#des").attr('cktype');
	var des_id = $("#des_id").val();
	$.post('/pub/Search/getSarchKeyCacheData',{keyword:keyword,searchType:'hotel',searchNum:searchNum,cktype:cktype,des_id:des_id},function(data){	
		if(searchNum != data.searchNum){
			return false;
		}
		var data = data.info;
		var html = structureHtml(data,'dest-sear-contxtB');	
		$(".keyword_B").html(html);
		$(".keyword_B").show();

	},'json')
});

//关键字搜索事件
$("#keyword").on('input',function(){
	if (!isLock) {
		$("#des").attr('cktype','area');
		$("#des_id1").val('');
		searchNum++;
	    if($('#keyword').val()==''){

	  //      	$(".keyword_A").show();
			// $(".keyword_B").hide();
			var keyword = '*';
	    }else{
	  //       $(".keyword_A").hide();
			// $(".keyword_B").show();
			var keyword = $('#keyword').val();
	    }

		var cktype = $("#des").attr('cktype');
		var des_id = $("#des_id").val();
		$.post('/pub/Search/getSarchKeyCacheData',{keyword:keyword,searchType:'hotel',searchNum:searchNum,cktype:cktype,des_id:des_id},function(data){	
			if(searchNum != data.searchNum){
				return false;
			}
			var data = data.info;
			var html = structureHtml(data,'dest-sear-contxtB');	
			$(".keyword_B").html(html);
			$(".keyword_B").show();

		},'json')
	  //   if($('#keyword').val()==''){
	  //      	$(".keyword_A").show();
			// $(".keyword_B").hide();
	  //   }else{
	  //       $(".keyword_A").hide();
			// $(".keyword_B").show();
	  //   }
	}
})

$(document).on("click","a[name=clearfix_a]",function(){
	$("#keyword").val($(this).html());
	$(".keyword_A").hide();
})

//构造ajax请求回来的数据
function structureHtml(data,cname){
	var ty = 0;	
	var html = '<div class="ih-sugbox ih-sugbox1"><div class=" destination-box"><ul class="dest-sear">';
	for(i in data){
		for(k in data[i]){
			type = data[i][k].type;
		}
        html += '<li class="clearfix">';
            html += '<div class="fl dest-sear-type">';
                html += ' <dl class="dest-sear-typecont"><dt class="sugic sugic'+(type == "hotel" ? 6 : (type == "site" ? 3 : 4))+'"></dt>';
                   html += ' <dd style="color: #333;">'+i+'</dd>';
                html += '</dl>';
            html += '</div>';
            html += '<div class="dest-sear-contbox">';
            for(k in data[i]){
            	var name = data[i][k].newName;
            	var lName = data[i][k].name;
            	var allName = data[i][k].allName;
            	var EnewName = data[i][k].EnewName;
            	var EallName = data[i][k].EallName;
            	
            	
                html += '<div class="dest-sear-cont" method="region" type="'+data[i][k].type+'" data-name="'+lName+'" data-regionid="'+data[i][k].id+'" data-id="'+data[i][k].id+'" data-regionname="'+name+'">';
                    html += '<div class="clearfix dest-sear-contxt" name="'+cname+'">';
                        html += '<div class="fl">';
                            html += '<p class="cname">'+name+'<span class="english-cname">【'+EnewName+'】</span></p>';
                            // html += '<p class="cname">'+allName;
                            html += '<p>'+allName+'<span class="english-cname">【'+EallName+'】</span></p>';
                        html += '</div>';
                        // html += '<div class="fr">';
                            // html += '<p><span class=" hnum">1982家酒店</span></p>';
                        // html += '</div>';
                    html += '</div>';
                html += '</div> ';          	
            }
        html += '</div> ';   
        html+='</li>';
		ty++;
	}	

	if(ty == 0){
		html += '<div class="ser-row-bot"><span style="text-decoration: none;">暫無搜索結果，換個條件試試</span></div>';	
	}
	html += ' </ul></div></div></div>';
	return html;
}




//根据键入关键词模糊查询地区、酒店
function getHotelData1(_this,keyword){
	var html = "";			
	$.post('/pub/Search/getSarchCacheData',{keyword:keyword,searchType:searchType},function(data){	
		var ty = 0;			
		for(i in data){
			// console.log(data);
			//循环最外层的类型 start
			html += '<div class="ser-row">';
			html+='<div class="ser-l-con" >';
				html+='<span class="ser-icon_region-hover">'+i+'</span>';
			html+='</div>';
			//循环最外层的类型 end
			//循环列表数据start
			var list = data[i];
				html +='<div class="ser-m-con">';
			for(k in list){
				if(k != 'info'){
					
					html+='<div class="ser-li ser-li-current" data-name="'+list[k].name+'" data-type="region" data-urlname="sicily" data-id="'+list[k].id+'">';
						html+='<span class="ser-li-l" style="float: left;">';
							html+='<a style="float: left;width: 100%;" cktype="'+list[k].type+'" onclick="clickScreen(this)" did="'+list[k].id+'" ><span class="t-ellip"><span  class="ser-m-con-bold">'+list[k].name+'</a>';
						html +='</span>';
					html +='</div>';													
				}					
			}
				html +='</div>';
			//循环列表数据 end	
			if(list.hasOwnProperty('info')){	//判断是否存在更多的数据
				//获取更多的数量 start 
				var info = list.info;
				var arr = Object.keys(info);
				var num = arr.length;						
				html +='<div class="ser-r-con" data-nums="'+num+'" ><span>更多'+num+'个</span>';								
					html +='<div class="ser-r-con-more hidden" style="top: 0px;">';
				
				//获取更多的数量 end 
						for(l in info){

							html +='<div class="ser-li" data-name="'+info[l].name+'" data-type="region" data-urlname="mississippi" data-id="'+info[l].id+'">';
								// html +='<span class="ser-li-l" style="float: left;">';
									html+='<a style="float: left;width: 100%;" onclick="clickScreen(this)" cktype="'+info[l].type+'" data-name="'+info[l].name+'" did="'+info[l].id+'" >'+info[l].name+'</a>';
								// html +='</span>';
							html +='</div>';										
						}

					html +='</div>';								
				html +='</div>';
				html +='<span class="clear"></span>';		

			}else{
				html +='<div class="ser-r-con" data-nums="1">';							
				
				html +='</div>';
				html +='<span class="clear"></span>';	
			}					
			html += '</div>';
			ty++;
		}	
		if(ty == 0){
			html += '<div class="ser-row-bot"><span style="text-decoration: none;">暫無搜索結果，換個條件試試</span></div>';	
		}
		// html += '<div class="ser-row-bot"><span style="text-decoration: none;">暫無搜索結果，換個條件試試</span></div>';				
		$('.input-search-select').html(html);
		$(".ser-r-con").mousemove(function(){
			$(this).addClass("ser-r-con-hover");
			$(this).find("div").removeClass("hidden");
		}).mouseout(function(){
			$(this).removeClass("ser-r-con-hover");
			$(this).find("div").addClass("hidden");
		})
		
		$(".se-r-con a").click(function(){
			$("#des").val($(this).attr("data-name"));
			$(".addr-city-box").hide();
		})
		
		$(".se-history-li").click(function(){
			$("#des").val($(this).attr("data-name"));
			$(".addr-city-box").hide();
		})
		$(".ser-l-con").css("height","32px");
		$(".ser-l-con").css("line-height","32px");
		$(".ser-r-con").css("height","32px");
		$(".ser-r-con").css("line-height","32px");

		// _this.$keyup.find('ul').html(html);
		
		_this.$keyup.show();
		_this.$pane.hide();
	},'json');			
}